home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / master1.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  3KB  |  102 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: master1.c,v 1.4 1997/07/09 13:25:09 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include "pvm3.h"
  35. #define SLAVENAME "slave1"
  36.  
  37. main()
  38. {
  39.     int mytid;                  /* my task id */
  40.     int tids[32];                /* slave task ids */
  41.     int n, nproc, numt, i, who, msgtype, nhost, narch;
  42.     float data[100], result[32];
  43.     struct pvmhostinfo *hostp;
  44.  
  45.     /* enroll in pvm */
  46.     mytid = pvm_mytid();
  47.  
  48.     /* Set number of slaves to start */
  49.     pvm_config( &nhost, &narch, &hostp );
  50.     nproc = nhost * 3;
  51.     if( nproc > 32 ) nproc = 32 ;
  52.     printf("Spawning %d worker tasks ... " , nproc);
  53.  
  54.     /* start up slave tasks */
  55.     numt=pvm_spawn(SLAVENAME, (char**)0, 0, "", nproc, tids);
  56.     if( numt < nproc ){
  57.        printf("\n Trouble spawning slaves. Aborting. Error codes are:\n");
  58.        for( i=numt ; i<nproc ; i++ ) {
  59.           printf("TID %d %d\n",i,tids[i]);
  60.        }
  61.        for( i=0 ; i<numt ; i++ ){
  62.           pvm_kill( tids[i] );
  63.        }
  64.        pvm_exit();
  65.        exit(1);
  66.     }
  67.     printf("SUCCESSFUL\n");
  68.     
  69.  
  70.     /* Begin User Program */
  71.     n = 100;
  72.     /* initialize_data( data, n ); */
  73.     for( i=0 ; i<n ; i++ ){
  74.        data[i] = 1.0;
  75.     }
  76.  
  77.     /* Broadcast initial data to slave tasks */
  78.     pvm_initsend(PvmDataDefault);
  79.     pvm_pkint(&nproc, 1, 1);
  80.     pvm_pkint(tids, nproc, 1);
  81.     pvm_pkint(&n, 1, 1);
  82.     pvm_pkfloat(data, n, 1);
  83.     pvm_mcast(tids, nproc, 0);
  84.  
  85.     /* Wait for results from slaves */
  86.     msgtype = 5;
  87.     for( i=0 ; i<nproc ; i++ ){
  88.        pvm_recv( -1, msgtype );
  89.        pvm_upkint( &who, 1, 1 );
  90.        pvm_upkfloat( &result[who], 1, 1 );
  91.        printf("I got %f from %d; ",result[who],who);
  92.        if (who == 0)
  93.             printf( "(expecting %f)\n", (nproc - 1) * 100.0);
  94.        else
  95.             printf( "(expecting %f)\n", (2 * who - 1) * 100.0);
  96.         
  97.     }
  98.     /* Program Finished exit PVM before stopping */
  99.     pvm_exit();
  100. }
  101.  
  102.